# Maintainer: Natanael Copa <ncopa@alpinlinux.org>
_luaversions="5.1 5.2 5.3"
pkgname=lua-alt-getopt
pkgver=0.7
pkgrel=8
pkgdesc="Lua module for processing options similar to getopt_long(3)"
url="https://github.com/LuaDist/alt-getopt"
arch="noarch"
license="MIT"
depends=""
makedepends=
install=
subpackages=""
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-alt-getopt:split_${_v/./_}"
done
source="alt-getopt-$pkgver.tar.gz::https://github.com/LuaDist/alt-getopt/archive/$pkgver.tar.gz
	lua5.2-compat.patch
	"

_builddir="$srcdir"/alt-getopt-$pkgver
prepare() {
	cd "$_builddir"
	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
		esac
	done
}

build() {
	cd "$_builddir"
}

package() {
	cd "$_builddir"
	for _i in $_luaversions; do
		mkdir -p "$pkgdir"/usr/share/lua/$_i
		install -Dm644 alt_getopt.lua \
			"$pkgdir"/usr/share/lua/$_i/alt_getopt.lua \
			|| return 1
	done
}

_split() {
	local d= _ver=$1
	pkgdesc="$pkgdesc for Lua $_ver"
	install_if="lua$_ver $pkgname=$pkgver-r$pkgrel"
	depends=
	for d in usr/lib/lua usr/share/lua; do
		if [ -d "$pkgdir"/$d/$_ver ]; then
			mkdir -p "$subpkgdir"/$d
			mv "$pkgdir"/$d/$_ver "$subpkgdir"/$d/ || return 1
		fi
	done
}


for _v in $_luaversions; do
	eval "split_${_v/./_}() { _split $_v; }"
done
sha512sums="8e7d1a2686a320a47199a71d09da1d3fad51bfa086dd6b1a95414325a24c66c4bef9ac9dd1ec9d866248a3b292a47e6c12cef47e88ce79a7f99bc5eaea68afc7  alt-getopt-0.7.tar.gz
9c4f44c811e1dd6164fb332bb95c23cff63f3347ee2e130b4bac08683a394cd3578425716a7cd49873ee35344d40414f03f5a30130c6a8fa4f94cd78cb0004f9  lua5.2-compat.patch"
